﻿requirejs.config({
    baseUrl: ".",
    paths: {
    	zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
        vue: "components/vue/vue.min",
        axios: "components/axios/axios.min",
        MINT: "components/mint-ui/index",
        moment: "components/moment/moment.min",
        common: "resources/js/common"
    },
    shim: {
        "MINT": {
            deps: ["vue"],
            exports: "MINT"
        }
    }
});

require(["zepto","dingtalk","vue", "axios", "moment", "common", "MINT"], function (zepto,dingtalk,Vue, axios, moment, common, MINT) {

    Vue.component(MINT.Navbar.name, MINT.Navbar);
    Vue.component(MINT.TabItem.name, MINT.TabItem);

    window.common = common;
    window.MINT = MINT;

    var vm = new Vue({
        el: "#view",
        data: {
            momentDate: moment(common.getParam("reportDate")),
            reportType: 1,
            reportInfo: [],
            description: "",
            showSubmit: false
        },
        computed: {
            reportDate: {
                set(newValue) {
                    this.momentDate = new moment(newValue);
                },
                get() {
                    return this.momentDate.format("YYYY-MM-DD");
                }
            },
            day: function () {
                return common.calendar.getDay(this.momentDate.format("d"));
            },
            year: function () {
                return this.momentDate.format("YYYY");
            },
            date: function () {
                return this.momentDate.format("MM.DD");
            }
        },
        watch: {
            reportType: function (val, oldVal) {
                this.reportChange();
            }
        },
        mounted: function () {
            this.reportType = common.getParam("reportType");
            this.reportChange();
        },
        methods: {
            addDay: function (value) {
                this.momentDate = moment(this.momentDate).add(value, "d");
                this.reportChange()
            },
            reportChange: function () {
                var self = this;

                axios.get("report/getIndex?reportType=" + this.reportType + "&reportDate=" + this.reportDate)
                    .then(function (response) {
                        self.showSubmit = common.isEmpty(response.data.description);
                        self.description = response.data.description;

                        self.reportInfo = [];
                        for (var name in response.data.data) {
                            self.reportInfo.push({
                                key: name,
                                value: response.data.data[name]
                            })
                        }
                    })
            },
            submit: function () {
                var self = this;

                if (common.isEmpty(this.description)) {
                    MINT.Toast("请输入工作说明");
                    return;
                }

                var data = {
                    reportType: this.reportType,
                    reportDate: this.reportDate,
                    description: this.description
                }

                axios.post("report/describe", data)
                    .then(function (response) {
                        self.showSubmit = false;

                        if (common.isEmpty(this.description)) {
                            MINT.Toast("工作说明提交成功");
                            return;
                        }
                    })

            }
        }
    });

});